<?php

class Admin_News_Controller extends Base_Controller {


	public $restful = true;

	public function __construct(){
		$this->_data = array();
		$this->model_news= new News();
	}

	public function get_index()
	{
		$per_page = Input::get('per_page', 20);
		$this->_data["news_list"] = $this->model_news->listNews($per_page);
//		var_dump($this->_data["news_list"]);exit;
		return View::make('admin.news.list')->with('news_list',$this->_data['news_list']);
	}


	public function action_addNews()
	{
		$this->_data["vp"] = $this->validate_parameter();
		$this->_data["input"] = Input::get();

		if($this->_data["vp"] === True ){
			$input = Input::get();
			$this->_data["state"]= $this->model_news->addNews($input);
		}

		var_dump($this->_data);
		//view
	}


	public function get_manage(){
	
		$new = array('title'=>'','content'=>'','sort_order'=>'','type'=>'','id'=>'');
		$id = Input::get('id');
		$this->_data["new"] = $this->model_news->getNews($id);
		if( !empty($this->_data["new"])){
			$new['title']=$this->_data["new"]->title;
			$new['content']=$this->_data["new"]->content;
			$new['sort_order']=$this->_data["new"]->sort_order;
			$new['type']=$this->_data["new"]->type;
			$new['id']=$this->_data["new"]->id;
		}
	
		 return View::make('admin.news.manage')->with('new',$new);
	
	}

/*

| id         | int(11) unsigned    | NO   | PRI | NULL              | auto_increment |
| title      | varchar(255)        | NO   |     | NULL              |                |
| content    | text                | NO   |     | NULL              |                |
| sort_order | int(11) unsigned    | NO   |     | 0                 |                |
| type       | tinyint(4) unsigned | NO   |     | 0                 |                |
| ctime      | timestamp           | YES  |     | CURRENT_TIMESTAMP |                |
| mtime      | int(11) unsigned    | NO   |     | 0                 |                |

 */
	public function post_manage(){
		$id = Input::get('id');
		$this->_data["input"] = Input::get();
		$this->_data["state"] = False;
		$this->_data["vp"] = $this->validate_parameter();
		$update_array = array();
		foreach(array('title','content','sort_order','type') as $key){
			$value = Input::get($key);
			if($value) $update_array[$key] = $value;
		}
		if( !empty($id)){
			//更新新闻
			if($this->_data["vp"] === True ){
				$this->_data["state"] = $this->model_news->updateNews($id,$update_array);
			}

		}else{
			//增加新闻
			if($this->_data["vp"] === True ){
				$this->_data["state"]= $this->model_news->addNews($update_array);
			}
		}
		if($this->_data["state"] == 1) $this->_data["state"]=true;
		else $this->_data["state"]=false;
		echo Response::json(array("state"=>$this->_data['state']));
	

	}

	public function action_updateNew()
	{
		$id = Input::get('id');
		foreach(array('title','content','sort_order','type') as $key){
			$value = Input::get($key);
			if($value) $update_array[$key] = $value;
		}
		$this->_data["state"] = $this->model_news->updateNews($id,$update_array);

		var_dump($this->_data);
	}

	public function post_del(){
		$id = Input::get('id');
		$this->_data["state"] = $this->model_news->deleteNews($id);
		if($this->_data["state"] == 1) $this->_data["state"]=true;
		else $this->_data["state"]=false;
		echo Response::json(array("state"=>$this->_data['state']));
	
	}
	public function action_deleteNews()
	{
		$id = Input::get('id');
		$this->_data["state"] = $this->model_news->deleteNews($id);
		var_dump($this->_data);
	}

	public function action_listNews()
	{
		$this->_data["news_list"] = $this->model_news->listNews();
		var_dump($this->_data);
	}

	public function action_getNews()
	{
		$id = Input::get('id');
		$this->_data["news"] = $this->model_news->getNews($id);
		var_dump($this->_data);
	}

	private function validate_parameter(){

		$input = Input::get();
		$rules = array(
			'title'  => 'required|max:50',
			'content' => 'required',
			'sort_order' => 'required',
			'type' => 'required'
		);

		$validation = Validator::make($input, $rules);

		if ($validation->fails())
		{
			return $validation->errors;
		}

		return True;	

	}

}
